<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class AdminRolesModel
{

    public static $status = ["" => '全部状态', 0 => '启用', 1 => '停用'];

    const ROLEDISABLE = 1;
    const ROLEENABLE = 0;
    const SUPERROLE = '*';


    public static function getAdminRolesList(array $params = array())
    {
        return self::getAdminRolesObj($params)->paginate(config('app.page_size'));
    }

    public static function getAllAdminRoles(array $params = array())
    {
        return self::getAdminRolesObj($params)->get();
    }

    public static function getAdminRolesExists(array $params = array())
    {
        return self::getAdminRolesObj($params)->exists();
    }

    public static function updateAdminRole(array $params = array(), array $update = array())
    {
        return self::getAdminRolesObj($params)->update($update);
    }

    public static function deleteAdminRoles(array $params = array())
    {
        return self::getAdminRolesObj($params)->delete();
    }

    public static function getAdminRoleData(array $params = array())
    {
        return self::getAdminRolesObj($params)->first();
    }

    public static function insertAdminRoles(array $insert = array())
    {
        $time = time();
        if (!isset($insert['create_time'])) {
            $insert['create_time'] = $time;
        }
        if (!isset($insert['update_time'])) {
            $insert['update_time'] = $time;
        }
        return DB::table('admin_roles')->insert($insert);
    }

    private static function getAdminRolesObj(array $params = array())
    {
        $adminRoles = DB::table('admin_roles');
        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $adminRoles = $adminRoles->whereIn('admin_roles.id', $params['id']);
            } else {
                $adminRoles = $adminRoles->where('admin_roles.id', '=', $params['id']);
            }
        }
        if (isset($params['name']) && !empty($params['name'])) {
            $adminRoles = $adminRoles->where('admin_roles.name', '=', $params['name']);
        }
        if (isset($params['status']) && $params['status'] !== "" && in_array($params['status'], [self::ROLEENABLE, self::ROLEDISABLE])) {
            $adminRoles = $adminRoles->where('admin_roles.status', '=', $params['status']);
        }
        if (isset($params['create_time']) && !empty($params['create_time'])) {
            $adminRoles = $adminRoles->where('admin_roles.create_time', '=', $params['create_time']);
        }
        if (isset($params['desc']) && !empty($params['desc'])) {
            $adminRoles = $adminRoles->where('admin_roles.desc', 'like', "%{$params['desc']}%");
        }
        if (isset($params['create_start_time']) && !empty($params['create_start_time'])) {
            $adminRoles = $adminRoles->where('admin_roles.create_time', '>=', $params['create_start_time']);
        }
        if (isset($params['create_end_time']) && !empty($params['create_end_time'])) {
            $adminRoles = $adminRoles->where('admin_roles.create_time', '<=', $params['create_end_time']);
        }
        if (isset($params['create_at']) && !empty($params['create_at'])) {
            $adminRoles = $adminRoles->where('admin_roles.create_at', '=', $params['create_at']);
        }
        return $adminRoles;
    }
}
